﻿using Nop.Data;
using Nop.Services.Customers;
using Nop.Services.Logging;
using Nop.Services.Tasks;

namespace Nop.Services.XJ
{
	public partial class SendMessageTask : ITask
	{
		//private readonly static string appId = "wx254193524213940e";
		//private readonly static string appSecret = "c020d4fcaceaef04d03194bcf408ba51";
		//private readonly static string templateId = "nQpdDl04GLhjY7EwrF3rk6orDUEI_WcDQ3lp5FUg09k";
		private readonly ICustomerService _customerService;
		private readonly IXJInspectionService _xJInspectionService;
		private readonly IDbContext _dbContext;
		private readonly ILogger _logger;
		public SendMessageTask(
			ICustomerService customerService,
			IXJInspectionService xJInspectionService,
			IDbContext dbContext,
			ILogger logger
			)
		{
			this._customerService = customerService;
			this._xJInspectionService = xJInspectionService;
			this._dbContext = dbContext;
			this._logger = logger;
		}

		public void Execute()
		{

			//// 更新未在规定时间执行的巡检记录状态为未执行
			//_xJInspectionService.UpdateExpire();

			//// 待通知的巡检记录
			//var ins = _xJInspectionService.GetAllInspection4Notice();
			//if (ins.Any())
			//{
			//	//AccessTokenContainer.Register(appId, appSecret);
			//	try
			//	{
			//		string page = "pages/inspection/detail";
			//		List<string> openIds = new List<string>();
			//		foreach (var item in ins)
			//		{
			//			string failIds = string.Empty;
			//			var templateMessageData = new TemplateMessageData
			//			{
			//				["thing1"] = new TemplateMessageDataValue(string.Format("巡检计划{0}", Enum.GetName(typeof(InspectionTypeEnum), item.TypeId))),
			//				["date2"] = new TemplateMessageDataValue(item.StartOn.ToString("yyyy年MM月dd日 HH:mm"))
			//			};

			//			if (item.PersonId > 0)
			//			{
			//				var customer = _customerService.GetCustomerById(item.PersonId);
			//				string openId = customer.GetAttribute<string>(SystemCustomerAttributeNames.OpenId);
			//				if (!string.IsNullOrEmpty(openId))
			//				{
			//					openIds.Add(openId);
			//				}
			//			}
			//			else
			//			{
			//				var dCustomers = _customerService.GetAllCustomers(departId: item.TeamId);
			//				if (dCustomers.Any())
			//				{
			//					foreach (var c in dCustomers)
			//					{
			//						string openId = c.GetAttribute<string>(SystemCustomerAttributeNames.OpenId);
			//						if (!string.IsNullOrEmpty(openId))
			//						{
			//							openIds.Add(openId);
			//						}
			//					}
			//				}
			//			}
			//			foreach (var openId in openIds)
			//			{
			//				var result = MessageApi.SendSubscribe(appId, openId, templateId, templateMessageData, page);
			//				if (result.errcode != ReturnCode.请求成功)
			//				{
			//					failIds += result.errmsg;
			//				}
			//			}
			//			_logger.Error(string.Format("发送失败：{0}", string.Join(",", failIds)));
			//		}

			//		_dbContext.ExecuteSqlCommand(string.Format("UPDATE XJInspection SET Notified = 1 WHERE Id IN ({0})", string.Join(",", ins.Select(x => x.Id).ToArray())));
			//	}
			//	catch (Exception ex)
			//	{
			//		_logger.Error(string.Format("发送失败：{0}", ex.Message));
			//	}
			//}

			//string url = _storeContext.CurrentStore.Url + "PublicProperty/SubscribeMessage";
			//HttpHelper httpHelper = new HttpHelper();
			//var s = httpHelper.BuildRequest(url, Newtonsoft.Json.JsonConvert.SerializeObject(new { sessionId = 1 }));




			//var appSettings = ConfigurationManager.AppSettings;
			//string ApiHost = appSettings["ApiHost"];
			//string Secret = appSettings["Secret"];
			//string Token = appSettings["Token"];
			//string TemplateId = appSettings["TemplateId"];
			//using (var client = new HttpClient())
			//{
			//	client.BaseAddress = new Uri(_storeContext.CurrentStore.Url);

			//	var requestJson = JsonConvert.SerializeObject(
			//	new
			//	{
			//		sessionId = 59,
			//	});

			//	HttpContent httpContent = new StringContent(requestJson);
			//	httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

			//	var result = client.PostAsync("/PublicProperty/SubscribeMessage", httpContent).Result.Content.ReadAsStringAsync().Result;
			//	_logger.Information(result);
			//}
		}
	}
}
